math.randomseed(os.time())


fsof_response = function(body)
	responses = responses + 1
--[[	
	if responses == 1 then
		wrk.thread:stop()
	end
	]]
	print("fsof body in lua:"..body)
	if body ~= nil and string.find(body,"ok") ==nil then
	--	orders = orders + 1
		print("failed response:"..body)
	end 
end


--file = io.open('traceid_uid.log', 'w')
fsof_request = function()
	requests = requests + 1
	amount =  math.random(5000)
	uid = math.random(3000000, 5090000)
	--uid = "3777777"
	local id1 = math.random(10000, 99999)
	--local id2 = math.random(10000, 99999)
	local traceid = "0A013020".. id1 .."2501568493"..uid 
	local req = {'{"environment": "pre","service": "com.fenqile.user.payway.service.IUserPaywayService","method": "getPaywayList","params": [{"ec_scene_type": 0,"order_amount": "'.. amount..'.00","ip_address": "202.105.138.94","rule_filter_flag": 1,"mac_code": "f68cb6b8bbd9ec8c","is_spec_pocket": "0","fp_ratio_list": ["0","5","10","20","30","40","50"],"stat_channel": "APP","sku_id": "MES201610180419136","channel_id": 3010,"order_type": "80"}],"group": "default","version": "1.0.0"}', '{"environment": "pre","service": "com.fenqile.user.payway.service.IUserPaywayService","method": "getPaywayStatus","types":["strings"],"params": [{"uid":"'.. uid ..'"}],"group": "default","version": "1.0.0"}', '{"service":"com.fenqile.user.payway.service.IUserPaywayService","environment":"pre-fenqile_itg","set":"empty","group":"default","version":"1.0.0","tracecontext":{"trace_id":"'..traceid.. '","rpc_id":"0.17","call_chain":"http://fm.fenqile.com[10.1.48.32:80]","log_params":[]},"method":"getDefaultPayway","types":["string"],"params":[{"get_period":0,"uid":"'.. uid ..'"}],"errcode":0,"errmsg":null}'}
    --request = req[math.random(#req)]
	request = req[3]
	--print('req:'.. request)
--	file:write(traceid .. ' '..uid.. '\n')
	return request
end

--[[
function delay()
   return math.random(10, 50)
end
]]

local threads = {}
local num = 1
function setup(thread)
   thread:set("id", num)
   thread:set("orders", orders)
   thread:set("tb", tb)
   table.insert(threads, thread)
   num = num + 1
end

function init(args)
   requests  = 0
   responses = 0
   orders = 0
   local msg = "thread %d created"
   print(msg:format(id))
end


function done(summary, latency, requests)
   for index, thread in ipairs(threads) do
      local id        = thread:get("id")
      local requests  = thread:get("requests")
      local responses = thread:get("responses")
	  local orders	  = thread:get("orders")
	--  local tb        = thread:get("tb")
      local msg = "thread %d made %d requests and got %d responses failed %d"
      print(msg:format(id, requests, responses, orders))
   end
	--dump2file(tb)
	--out:close()
end
